var conData = require('MinesConstants');
var Logic = {

    shuffle : function(aArr)
    {
        var iLength = aArr.length,
            i = iLength,
            mTemp,
            iRandom;

        while(i--){
            if(i !== (iRandom = Math.floor(Math.random() * iLength))){
                mTemp = aArr[i];
                aArr[i] = aArr[iRandom];
                aArr[iRandom] = mTemp;
            }
        }
        return aArr;
    },


    generBoardData : function(minesNum)
    {
        var maxBoardNum = conData.MAX_ROW*conData.MAX_COL - conData.MAX_ROW/2;
        var numLis = [];
        for(var i=0;i<maxBoardNum;i++)
        {
            numLis[i] = (i<minesNum ? conData.MINE : 0);
        }
        numLis = this.shuffle(numLis);

        var data = [];
        for(var i=0;i<conData.MAX_ROW;i++)
        {
            var endLen=  (i %2 == 0 ? conData.MAX_COL :conData.MAX_COL -1);
            data[i] = numLis.splice(0,endLen);
        }

        for(var i=0;i<data.length;i++)
        {
            var lis = data[i];
            for(var j=0;j<lis.length;j++)
            {
                if(lis[j] == -1) continue;
                var legalLis = this.getLegaldjacentCoord(i,j);
                for(var k=0;k<legalLis.length;k++)
                {
                    var row = legalLis[k][0];
                    var col = legalLis[k][1];
                    if(data[row][col] < 0)
                    {
                        var curNum = data[i][j];
                        data[i][j] = (curNum < 0 ? 1 : curNum+1);
                    }
                }
            }
        }

        return data;
    },


    //获取合法相邻坐标
    getLegaldjacentCoord:function(row,col)
    {
        var lis = [];
        var tL = [row-1,row % 2 ==0 ? col-1 :col];
        var tR = [row-1,row % 2 ==0 ? col :col+1];

        var l = [row,col-1];
        var r = [row,col+1];

        var bL = [row+1,tL[1]];
        var bR = [row+1,tR[1]];

        var curColLen = conData.MAX_COL-1;
        var topLen = conData.MAX_COL;
        var bottomLen = conData.MAX_COL;
        if(row % 2 == 0)
        {
            curColLen = conData.MAX_COL;
            topLen = conData.MAX_COL-1;
            bottomLen = conData.MAX_COL-1;
        }

        if(tL[0] >= 0)
        {
            if(tL[1] >= 0) lis.push(tL);
            if(tR[1] < topLen) lis.push(tR);
        }

        if(l[0] >= 0)
        {
            if(l[1] >= 0) lis.push(l);
            if(r[1] < curColLen) lis.push(r);
        }

        if(bL[0] < conData.MAX_ROW)
        {
            if(bL[1] >= 0) lis.push(bL);
            if(bR[1] < bottomLen) lis.push(bR);
        }
        return lis;
    },


    contains:function(arr2, arr)
    {
         var i = arr2.length;
         while (i--)
         {
             var arr1 = arr2[i];
             var isEqul = arr1.length > 0;
             for(var k=0;k<arr1.length;k++)
             {
                 if(arr1[k] !== arr[k])
                 {
                     isEqul = false;
                 }
             }
             if(isEqul)
             {
                 return true;
             }
         }
        return false;
    },

    //坐标xy 二维数组 返回新增的显示列表
    isTouchInMine: function(coordX,coordY,curDataArray2,newShowLis)
    {
        this.getLegaldjacentCoord(3,8);
        var val = curDataArray2[coordX][coordY];
        newShowLis.push([coordX,coordY]);
        if(val == conData.MINE)
        {
            return true;
        }
        else
        {
            if(val == 0)
            {
                var lis = this.getLegaldjacentCoord(coordX,coordY);
                for(var i=0;i<lis.length;i++)
                {
                    var row = lis[i][0];
                    var col = lis[i][1];
                    if(this.contains(newShowLis,lis[i]) == false)
                    {
                        this.isTouchInMine(row,col,curDataArray2,newShowLis);
                    }
                }
            }
            //else
            //{
            //    var lis = this.getLegaldjacentCoord(coordX,coordY);
            //    for(var i=0;i<lis.length;i++)
            //    {
            //        var row = lis[i][0];
            //        var col = lis[i][1];
            //        var nextVal = curDataArray2[row][col];
            //        if(nextVal == 0)
            //        {
            //            if(this.contains(newShowLis,lis[i]) == false)
            //            {
            //                this.isTouchInMine(row,col,curDataArray2,newShowLis);
            //            }
            //        }
            //    }
            //}
            return false;
        }
    },
}

module.exports = Logic;